package com.hula.onion.book.server.spider.platforms.wangcai51;

import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.SpiderCommon;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import com.hula.onion.book.server.utils.HttpUtils;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/17 19:33
 *
 * @author shan
 * @version 1.0
 */
@Component("wangCai51Spider")
public class WangCai51Spider extends SpiderCommon {
    /**
     * 登录URL
     */
    private static final String LOGIN_URL = "https://www.91wangcai.com/oauth2/authorize";
    /**
     * 获取账户信息URL
     */
    private static final String GET_ACCOUNT_INFO_URL = "https://www.91wangcai.com/mobile/v2/wc_user_account";

    /**
     * 获取验证码URL
     */
    private static final String GET_CODE_INFO_URL = "https://www.91wangcai.com/captcha/image";


    @Autowired
    private WangCai51SpiderHelper niWoDaiSpiderHelper;

    /**
     * 执行爬取操作
     * 1.获取 RSA key
     * 2.组装模拟登陆参数，模拟登陆
     * 3.爬取用户详细信息
     */
    @Override
    public CommonResponse execute() {

        CommonResponse commonResponse = new CommonResponse(ResponseStatusEnum.FAIL);
        //1.1 组装模拟登陆参数
        HttpClientContext httpContext = HttpClientContext.create();
        BasicCookieStore cookieStore = new BasicCookieStore();
        httpContext.setCookieStore(cookieStore);
        try {
            Map<String, Object> params =
                    niWoDaiSpiderHelper.installRequestParams(loginForm, "");

            //1.2 模拟登陆
            String responseStr = HttpUtils.post(LOGIN_URL, params,httpContext);
            WangCai51ResponseEntity responseEntity = JSONObject.parseObject(responseStr, WangCai51ResponseEntity.class);

            logger.info("[wangCai51Spider] Http response result:{}", responseEntity.toString());

            if ("1".equals(responseEntity.getStatus())) {
                //2.1 爬取用户详细信息
                String result = HttpUtils.get(GET_ACCOUNT_INFO_URL,httpContext);
                logger.info("[wangCai51Spider] Http response account result:{}", result);
                //2.2 解析JSON
                AccountInfo accountBalance = niWoDaiSpiderHelper.parseAccountBalance(result);

                commonResponse = new CommonResponse(ResponseStatusEnum.SUCCESS);
                commonResponse.setData(accountBalance);

            } else {
                commonResponse.setMsg(responseEntity.getData());
                commonResponse.setCode(responseEntity.getStatus());
            }

        } catch (Exception e) {
            logger.error("[wangCai51Spider] Install request params error!");
        }

        return commonResponse;
    }
}
